#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class WaterElectricityFeesProvider : CY.GFive.Core.Providers.DALProvider.IWaterElectricityFeesProvider
	{
		#region Sql
		private static readonly string SqlSelectWaterElectricityFees = "SELECT [Id], [RcdCate],[RoomNbr],[Deposit],[BuildingNbr],[YearNum],[MonthNum] FROM [WaterElectricityFees] WHERE  [Id]=@Id";

		private static readonly string SqlInsertWaterElectricityFees = "INSERT INTO [WaterElectricityFees]([RcdCate],[RoomNbr],[Deposit],[BuildingNbr],[YearNum],[MonthNum]) values(@RcdCate,@RoomNbr,@Deposit,@BuildingNbr,@YearNum,@MonthNum);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateWaterElectricityFees = "UPDATE [WaterElectricityFees] SET [RcdCate] = @RcdCate,[RoomNbr] = @RoomNbr,[Deposit] = @Deposit,[BuildingNbr] = @BuildingNbr,[YearNum] = @YearNum,[MonthNum] = @MonthNum WHERE  [Id]=@Id";

		private static readonly string SqlDeleteWaterElectricityFees = "DELETE [WaterElectricityFees] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllWaterElectricityFees = "SELECT [Id], [RcdCate],[RoomNbr],[Deposit],[BuildingNbr],[YearNum],[MonthNum] FROM [WaterElectricityFees]";
		#endregion

		#region IWaterElectricityFeesProvider Members
		public Core.Business.WaterElectricityFees Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectWaterElectricityFees);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.WaterElectricityFees waterElectricityFees = new Core.Business.WaterElectricityFees();

				if (!reader.IsDBNull(0)) waterElectricityFees.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) waterElectricityFees.RcdCate = reader.GetString(1);
				if (!reader.IsDBNull(2)) waterElectricityFees.RoomNbr = reader.GetString(2);
				if (!reader.IsDBNull(3)) waterElectricityFees.Deposit = reader.GetDecimal(3);
				if (!reader.IsDBNull(4)) waterElectricityFees.BuildingNbr = reader.GetString(4);
				if (!reader.IsDBNull(5)) waterElectricityFees.YearNum = reader.GetString(5);
				if (!reader.IsDBNull(6)) waterElectricityFees.MonthNum = reader.GetInt32(6);

				return waterElectricityFees;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.WaterElectricityFees waterElectricityFees)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@RcdCate", SqlDbType.NVarChar, waterElectricityFees.RcdCate);
			sql.AddParameter("@RoomNbr", SqlDbType.NVarChar, waterElectricityFees.RoomNbr);
			sql.AddParameter("@Deposit", SqlDbType.Decimal, waterElectricityFees.Deposit);
			sql.AddParameter("@BuildingNbr", SqlDbType.NVarChar, waterElectricityFees.BuildingNbr);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, waterElectricityFees.YearNum);
			sql.AddParameter("@MonthNum", SqlDbType.Int, waterElectricityFees.MonthNum);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertWaterElectricityFees);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) waterElectricityFees.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.WaterElectricityFees waterElectricityFees)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@RcdCate", SqlDbType.NVarChar, waterElectricityFees.RcdCate);
			sql.AddParameter("@RoomNbr", SqlDbType.NVarChar, waterElectricityFees.RoomNbr);
			sql.AddParameter("@Deposit", SqlDbType.Decimal, waterElectricityFees.Deposit);
			sql.AddParameter("@BuildingNbr", SqlDbType.NVarChar, waterElectricityFees.BuildingNbr);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, waterElectricityFees.YearNum);
			sql.AddParameter("@MonthNum", SqlDbType.Int, waterElectricityFees.MonthNum);
			sql.AddParameter("@Id", SqlDbType.Int, waterElectricityFees.Id);
			sql.ExecuteSql(SqlUpdateWaterElectricityFees);
		}

		public void Delete(Core.Business.WaterElectricityFees waterElectricityFees)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, waterElectricityFees.Id);
			sql.ExecuteSql(SqlDeleteWaterElectricityFees);
		}

		public IList<Core.Business.WaterElectricityFees> GetAllWaterElectricityFees()
		{
			IList<Core.Business.WaterElectricityFees> waterElectricityFeeslist = new List<Core.Business.WaterElectricityFees>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllWaterElectricityFees);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.WaterElectricityFees waterElectricityFees = new Core.Business.WaterElectricityFees();

					if (!reader.IsDBNull(0)) waterElectricityFees.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) waterElectricityFees.RcdCate = reader.GetString(1);
					if (!reader.IsDBNull(2)) waterElectricityFees.RoomNbr = reader.GetString(2);
					if (!reader.IsDBNull(3)) waterElectricityFees.Deposit = reader.GetDecimal(3);
					if (!reader.IsDBNull(4)) waterElectricityFees.BuildingNbr = reader.GetString(4);
					if (!reader.IsDBNull(5)) waterElectricityFees.YearNum = reader.GetString(5);
					if (!reader.IsDBNull(6)) waterElectricityFees.MonthNum = reader.GetInt32(6);

					waterElectricityFees.MarkOld();
					waterElectricityFeeslist.Add(waterElectricityFees);
				}
				reader.Close();
			}
			return waterElectricityFeeslist;
		}
		#endregion
	}
}
